/**
 * 初始化销售配套详情对话框
 */
var SaleInfoDlg = {
    saleInfoData : {},
    validateFields: {
        customerCompanyName: {
            validators: {
                notEmpty: {
                    message: '企业客户必选'
                }
            }
        },
        product: {
            validators: {
                notEmpty: {
                    message: '不能为空'
                }
            }
        },
        year: {
            validators: {
                notEmpty: {
                    message: '不能为空'
                }
            }
        },
        customerName:{
            validators:{
                notEmpty: {
                    message: '不能为空'
                }
            }
        },
        annualSupply: {
            validators: {
                notEmpty: {
                    message: '不能为空'
                },
                numeric:{
                    message: "必须为数字"
                }
            }
        },
        accountReceivableBalance:{
            validators:{
                numeric:{
                    message: "必须为数字"
                }
            }
        },
        creditBalance:{
            validators:{
                numeric:{
                    message: "必须为数字"
                }
            }
        },
        cashReceipted:{
            validators:{
                numeric:{
                    message: "必须为数字"
                }
            }
        },
        silverTicketRecivied:{
            validators:{
                numeric:{
                    message: "必须为数字"
                }
            }
        },
        businessTicketRecivied:{
            validators:{
                numeric:{
                    message: "必须为数字"
                }
            }
        },
        period:{
            validators:{
                digits:{
                    message: "必须为整数"
                }
            }
        },
        businessTicketDiscount:{
            validators:{
                numeric:{
                    message: "必须为数字"
                }
            }
        },
        discountInterestRate:{
            validators:{
                numeric:{
                    message: "必须为数字"
                }
            }
        },
    }
};

/**
 * 清除数据
 */
SaleInfoDlg.clearData = function() {
    this.saleInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
SaleInfoDlg.set = function(key, val) {
    this.saleInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
SaleInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
SaleInfoDlg.close = function() {
    parent.layer.close(window.parent.Sale.layerIndex);
}

/**
 * 收集数据
 */
SaleInfoDlg.collectData = function() {
    this
    .set('id')
    .set('customerCompanyId')
    .set('year')
    .set('product')
    .set('customerName')
    .set('annualSupply')
    .set('accountReceivableBalance')
    .set('creditBalance')
    .set('cashReceipted')
    .set('silverTicketRecivied')
    .set('businessTicketRecivied')
    .set('period')
    .set('businessTicketDiscount')
    .set('discountInterestRate');
}
/**
 * 验证数据是否为空
 * @returns {*|jQuery}
 */
SaleInfoDlg.validate = function () {
    $('#saleContent').data("bootstrapValidator").resetForm();
    $('#saleContent').bootstrapValidator('validate');
    return $("#saleContent").data('bootstrapValidator').isValid();
}

/**
 * 提交添加
 */
SaleInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();
    if(!this.validate()){
       return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/sale/add", function(data){
        Feng.success("添加成功!");
        window.parent.Sale.table.refresh();
        SaleInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.saleInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
SaleInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();
    if(!this.validate()){
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/sale/update", function(data){
        Feng.success("修改成功!");
        window.parent.Sale.table.refresh();
        SaleInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.saleInfoData);
    ajax.start();
}


SaleInfoDlg.showCustomerCompanySelectTree = function () {
    Feng.showInputTree("customerCompanyName", "customerCompanyTreeDiv", 15, 34);
};

SaleInfoDlg.onClickCustomerCompany = function (e, treeId, treeNode) {
    $("#customerCompanyName").attr("value", treeNode.name);
    $("#customerCompanyId").attr("value", treeNode.id);
    $("#customerCompanyTreeDiv").fadeOut("fast");
}


$(function() {
    Feng.initValidator("saleContent",SaleInfoDlg.validateFields);

    var customerCompanyTree = new $ZTree("customerCompanyTree", "/customerCompany/tree");
    customerCompanyTree.bindOnClick(SaleInfoDlg.onClickCustomerCompany);
    customerCompanyTree.init();
});
